﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Emoney.UserTraining.Model;
using Emoney.UserTraining.DAL;
using System.Text;
using Emoney.UserTraining.Cache;

namespace Emoney.UserTraining.BLL
{
    public class Web_TrainDmBLL
    {
        TrainDAL dal = new TrainDAL();

        public List<DmTrain> GetTarget(string areaCN)
        {
            List<DmTrain> lst1 = new List<DmTrain>();
            string key = "dmtrainTarget_" + areaCN.Trim();
            News2CacheTools cache = new News2CacheTools();
            var obj = cache.Get<List<DmTrain>>(key);
            if (obj == null)
            {
                Web_Train t = new Web_Train();
                t.sign = 0;
                t.area = areaCN;
                List<DmTrain> lst = new List<DmTrain>();
                DataSet ds = dal.GetTrainTarget(t);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        for (var i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            DmTrain c = new DmTrain();
                            c.id = ds.Tables[0].Rows[i]["id"].ToString();
                            c.place_num = ds.Tables[0].Rows[i]["place_num"].ToString();
                            c.targetname = ds.Tables[0].Rows[i]["train_target"].ToString();
                            c.place_name = ds.Tables[0].Rows[i]["place_name"].ToString();
                            c.teachername = ds.Tables[0].Rows[i]["teachername"].ToString();
                            c.train_time = ds.Tables[0].Rows[i]["train_time"].ToString();
                            c.teacherimg = ds.Tables[0].Rows[i]["teacherimg"].ToString();
                            c.teacherintro = ds.Tables[0].Rows[i]["teacherintro"].ToString();
                            c.remark = ds.Tables[0].Rows[i]["remark"].ToString();
                            lst.Add(c);
                        }
                    }
                }
                lst1 = lst;
                cache.Add(key, lst1, 0, 1, 0);
            }
            else
            {
                lst1 = obj;
            }
            return lst1;
        }
        public List<DmTrain> ShowDmTrain(string areaCN, string tid)
        {
             List<DmTrain> lst1 = new List<DmTrain>();
              string key = "dmtrainShow_" + areaCN.Trim() + tid.Trim();
             News2CacheTools cache = new News2CacheTools();
             var obj = cache.Get<List<DmTrain>>(key);
             if (obj == null)
             {
                StringBuilder sb = new StringBuilder();
                Web_Train t = new Web_Train();
                t.sign = 0;
                t.area = areaCN;
                DataSet ds = dal.GetTrainTarget(t);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = ds.Tables[0];
                        if (tid != null && tid.ToString() != "")
                        {
                            t.id = int.Parse(tid.ToString());
                        }
                        else
                        {
                            t.id = int.Parse(dt.Rows[0]["id"].ToString());
                        }
                    }
                }
                t.sign = 1;
                DataTable dt1 = dal.GetTrainTarget(t).Tables[0];
                List<DmTrain> lst = new List<DmTrain>();
                if (dt1.Rows.Count > 0)
                {
                    DmTrain c = new DmTrain();
                    c.id = dt1.Rows[0]["id"].ToString();
                    c.num = int.Parse(dt1.Rows[0]["num"].ToString());
                    c.allnum = int.Parse(dt1.Rows[0]["allnum"].ToString());
                    c.place_name = dt1.Rows[0]["place_name"].ToString();
                    c.teachername = dt1.Rows[0]["teachername"].ToString();
                    c.train_time = dt1.Rows[0]["train_time"].ToString();
                    c.train_intro = dt1.Rows[0]["train_intro"].ToString();
                    c.train_style = dt1.Rows[0]["train_style"].ToString();
                    c.train_addr = dt1.Rows[0]["train_addr"].ToString();
                    c.teacherintro = dt1.Rows[0]["teacherintro"].ToString();
                    c.place_num = dt1.Rows[0]["place_num"].ToString();
                    c.remark = dt1.Rows[0]["remark"].ToString();
                    lst.Add(c);
                }
                lst1 = lst;
                cache.Add(key, lst1, 0, 1, 0);
             }
             else
             {
                 lst1 = obj;
             }
            return lst1;
        }
    }
}